<?php
class Message extends AdminAction{
	function index(){
		template('messagelist',$this->data);
	}
	
	function messagelist(){
		$db=$this->getDB();
		$page = P('page');
		$rows = P('rows');
		$result = array();
		$t = $db->get_assoc("select count(*) total from message where lang = '".$this->lang."'");
		$result['total'] = $t['total'];
		if(empty($page)||empty($rows)){
			$r = $db->query_assoc("select id,title,content,lang,createtime,faceimg,leaver,sex,phonenumber,compname,inwhere,address,hasreply,date_format(createtime,'%Y-%c-%d') createtime,reply from message where lang = '".$this->lang."' order by createtime desc limit 0,10");
			$result['rows'] = $r;
		}else{
			$start = ($page-1)*$rows;
			$r = $db->query_assoc("select id,title,content,lang,createtime,faceimg,leaver,sex,phonenumber,compname,inwhere,address,hasreply,date_format(createtime,'%Y-%c-%d') createtime,reply from message where lang = '".$this->lang."' order by createtime desc limit $start,$rows ");
			$result['rows'] = $r;
		}
		echo json_encode($result);
	}
	
	function messageupdate(){
		$db=$this->getDB();
		$id = P('id');
		$title = P('title');
		$content = P('content');
		$content = addslashes($content);
		
		$db->query("update message set title = '$title',content='$content' where id = $id");
		
	 	$message = array();
		$message = setMessage(true,'留言修改成功！',$message);
		echo json_encode($message);
	}
	
	function messagedelete(){
		$db=$this->getDB();
		$id = P('id');
		
		$db->query("delete from message where id = $id");
		
		$message = array();
		$message = setMessage(true,'留言删除成功！',$message);
		echo json_encode($message);
	}
	
	function addreply(){
		$db=$this->getDB();
		$mid = P('mid');
		$rep = P('rep');
		$rep = addslashes($rep);
		
		$db->query("update message set reply ='$rep',hasreply = 1 where id = $mid ");
		
		$message = setMessage(true,'留言编辑成功！',$message);
		echo json_encode($message);
		
	}
	
	
}